package com.ruoyi.web.controller.zdgl;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zdgl.domain.YwDdxxbExport;
import com.ruoyi.zdgl.domain.YwDdxxbVo;
import com.ruoyi.zdgl.domain.YwGsxxb;
import com.ruoyi.zdgl.service.IYwXmxxbService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zdgl.domain.YwDdxxb;
import com.ruoyi.zdgl.service.IYwDdxxbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 *
 * @author chrui
 * @date 2024-07-27
 */
@RestController
@RequestMapping("/zdgl/ddxxb")
public class YwDdxxbController extends BaseController {


    @Autowired
    private IYwDdxxbService ywDdxxbService;

    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('zdgl:ddxxb:list')")
    @GetMapping("/list")
    public TableDataInfo list(YwDdxxb ywDdxxb)
    {
        startPage();
        List<YwDdxxb> list = ywDdxxbService.selectYwDdxxbList(ywDdxxb);
        return getDataTable(list,ywDdxxbService.selectYwDdxxbListCount(ywDdxxb));
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('zdgl:ddxxb:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YwDdxxb ywDdxxb)
    {
        List<YwDdxxbExport> list = ywDdxxbService.selectYwDdxxbListExport(ywDdxxb);
        ExcelUtil<YwDdxxbExport> util = new ExcelUtil<YwDdxxbExport>(YwDdxxbExport.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 计算总计
     */
    @PostMapping("/calDdzj")
    public AjaxResult calDdzj(@RequestBody YwDdxxb ywDdxxb) {
        BigDecimal dj = ywDdxxb.getDj();
        BigDecimal sl = ywDdxxb.getSl();
        BigDecimal zero = new BigDecimal("0");
        if(dj==null||sl==null){
            return error("单价和数量未填写");
        }
        if (dj.compareTo(zero) <= 0) {
            return error("单价应该大于0");
        }
        if (sl.compareTo(zero) <= 0) {
            return error("数量应该大于0");
        }
        BigDecimal zj = new BigDecimal("0");
        BigDecimal zjPerm =  NumberUtil.mul(dj, sl);
        String dictValue = DictUtils.getDictValue("jesfsswr", "jesfsswr");
        if(StringUtils.isNotEmpty(dictValue)){
            if(dictValue.equals("1")){
                zj = NumberUtil.roundHalfEven(zjPerm, 2);
            }else{
                zj = zjPerm;
            }
        }else{
            zj = zjPerm;
        }
        return success(zj);
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('zdgl:ddxxb:query')")
    @GetMapping(value = "/{ddbh}")
    public AjaxResult getInfo(@PathVariable("ddbh") Long ddbh) {
        return success(ywDdxxbService.selectYwDdxxbByDdbh(ddbh));
    }

    /**
     * 新增订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addDdxxbByText")
    public AjaxResult addDdxxbByText(@RequestBody YwDdxxbVo ywDdxxb) {
        ywDdxxb.setCreateBy(getUsername());
        return toAjax(ywDdxxbService.addDdxxbByText(ywDdxxb));
    }

    /**
     * 校验订单信息
     */
    @Log(title = "校验订单信息", businessType = BusinessType.OTHER)
    @PostMapping(value = "/checkDdxxbByText")
    public AjaxResult checkDdxxbByText(@RequestBody YwDdxxbVo ywDdxxb) {
        return success("返回成功",ywDdxxbService.checkDdxxbByText(ywDdxxb));
    }

    /**
     * 行内编辑订单信息
     */
    @Log(title = "行内编辑订单信息", businessType = BusinessType.OTHER)
    @PostMapping(value = "/updateDdxxbByColumn")
    public AjaxResult updateDdxxbByColumn(@RequestBody YwDdxxb ywDdxxb) {
        return success("返回成功",ywDdxxbService.updateDdxxbByColumn(ywDdxxb));
    }

    /**
     * 新增订单信息 文本格式
     */
    @PreAuthorize("@ss.hasPermi('zdgl:ddxxb:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YwDdxxb ywDdxxb) {
        ywDdxxb.setCreateBy(getUsername());
        return toAjax(ywDdxxbService.insertYwDdxxb(ywDdxxb));
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('zdgl:ddxxb:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YwDdxxb ywDdxxb) {
        ywDdxxb.setUpdateBy(getUsername());
        return toAjax(ywDdxxbService.updateYwDdxxb(ywDdxxb));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('zdgl:ddxxb:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ddbhs}")
    public AjaxResult remove(@PathVariable Long[] ddbhs) {
        return toAjax(ywDdxxbService.deleteYwDdxxbByDdbhs(ddbhs));
    }
}
